//
//  LS_AdvertiseItemsTableViewCell.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/10.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_AdvertiseItemsTableViewCell.h"
#import "BZFireworkAnimationButton.h"
#import "LS_ActivitysModel.h"
#import "LImageListView.h"
#import "LS_FindViewModel.h"
// 顶部和底部的留白
#define kBlank              15
// 头像视图的宽、高
#define kFaceWidth          40
// 内容视图宽度
#define kTextWidth     (ScreenWidth-20)
// 视图之间的间距
#define kPaddingValue       8

#define KBottomBtnWinth    80
#define KBottomBtnHeight   20

@interface LS_AdvertiseItemsTableViewCell()
@property(nonatomic,strong)UIImageView *lwc_headImageView;
@property(nonatomic,strong)UILabel *lwc_nameLabel;
@property(nonatomic,strong)UILabel *lwc_timeLabel;
@property(nonatomic,strong)UIButton *lwc_deleteBtn;
@property(nonatomic,strong)UILabel *lwc_linkLabel;
@property(nonatomic,strong)UIView *lwc_lineView;
@property(nonatomic,strong)BZFireworkAnimationButton *praiseButton;
@property(nonatomic,strong)HJLayoutBtn *lwc_prasieButton;
@property(nonatomic,strong)HJLayoutBtn *lwc_commentButton;
@property(nonatomic,strong)LImageListView *lwc_listView;
@end

@implementation LS_AdvertiseItemsTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
      
        [[self.lwc_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            NSLog(@"点击了删除");
        }];
        
       [[self.lwc_commentButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
             NSLog(@"点击了评论");
        }];
        
    }return self;
}
-(void)hideButton
{
    self.praiseButton.hidden = YES;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    
}
#pragma mark -
#pragma mark --绑定View的数据 
- (void)bindViewModel:(id)viewModel withParams:(NSDictionary *)params
{
    NSLog(@"viewModel----%@------params-----%@",viewModel,params);
//  LS_ActivitysModel *model = viewModel[[params[@"index"] integerValue]];
    LS_FindViewModel *mainModel = viewModel;
    LS_ActivitysModel *model = mainModel.advertisesItem[[params[@"index"] integerValue]];
    //头像
    [self.lwc_headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.head]] placeholderImage:[UIImage imageNamed:@"erweima"]];
    
    //昵称
    self.lwc_nameLabel.text = model.user_name;
    self.lwc_timeLabel.text = model.add_time;
    //内容
    CGFloat bottom=CGRectGetMaxY(self.lwc_headImageView.frame) + kPaddingValue;
    CGFloat rowHeight=0;//cell高度
    
    if ([model.content length]) {

        self.lwc_linkLabel.text = model.content;
        CGFloat labheight = [model.content heightForFont:LS_Font(13) width:kTextWidth];
        
        self.lwc_linkLabel.frame=CGRectMake(10, bottom, kTextWidth, labheight);
        bottom=CGRectGetMaxY(self.lwc_linkLabel.frame)+kPaddingValue;
    }
    
    
   
    if (![model.pic isKindOfClass:[NSNull class]]) {
        self.lwc_listView.lwc_imageArr = model.pic ;
        if (model.pic.count>0) {
            [self.lwc_listView setFrame:CGRectMake(10, bottom, self.lwc_listView.width, self.lwc_listView.height)];
            bottom=self.lwc_listView.bottom+kPaddingValue;
        }
    }
    
    //分割线
    self.lwc_lineView.frame = CGRectMake(0, bottom, ScreenWidth, 0.5);
    //赞
    self.praiseButton.frame=CGRectMake(ScreenWidth-10-KBottomBtnWinth*2-10, CGRectGetMaxY(self.lwc_lineView.frame) + kPaddingValue, KBottomBtnWinth, KBottomBtnHeight);
    self.lwc_prasieButton.frame=CGRectMake(ScreenWidth-10-KBottomBtnWinth*2-10, CGRectGetMaxY(self.lwc_lineView.frame) + kPaddingValue, KBottomBtnWinth, KBottomBtnHeight);
    [self.praiseButton setTitle:model.like_num forState:UIControlStateNormal];
    [self.lwc_prasieButton setTitle:model.like_num forState:UIControlStateNormal];
    if ([[NSString stringWithFormat:@"%@",model.status] isEqualToString:@"1"]) {
        self.lwc_prasieButton.selected=YES;
        self.praiseButton.selected = YES;
    }else{
        self.lwc_prasieButton.selected=NO;
        self.praiseButton.selected = NO;
    }
   
    //评论
    self.lwc_commentButton.frame=CGRectMake(ScreenWidth-10-KBottomBtnWinth, CGRectGetMaxY(self.lwc_lineView.frame) + kPaddingValue, KBottomBtnWinth, KBottomBtnHeight);
    [self.lwc_commentButton setTitle:model.comment_num forState:UIControlStateNormal];
    
    
    rowHeight=CGRectGetMaxY(self.lwc_commentButton.frame) + kPaddingValue;
    // 这样做就是起到缓存行高的作用，省去重复计算!!!
    model.rowHeight = rowHeight;
    
    
    [[[[self.lwc_prasieButton rac_signalForControlEvents:UIControlEventTouchUpInside] doNext:^(id x) {
        
        NSLog(@"111*******%@",x);
    }] flattenMap:^RACStream *(id value) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
           NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
            dict[@"model"]  = model;
            dict[@"button"] = self.lwc_prasieButton;
            dict[@"BZFireworkbtn"] = self.praiseButton;
           [mainModel.pariaZanCommand execute:dict];
           return nil;
        }];
    }] subscribeNext:^(id x) {
        
        NSLog(@"222*******%@",x);
    }]; 
}
-(UIImageView *)lwc_headImageView
{
    return LS_LAZY(_lwc_headImageView, ({
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, kBlank, kFaceWidth, kFaceWidth)];
        imageView.contentMode=UIViewContentModeScaleToFill;
        imageView.clipsToBounds=YES;
        imageView.layer.cornerRadius = kFaceWidth/2;
        imageView.userInteractionEnabled=YES;
        [self.contentView addSubview:imageView];
        imageView;
    }));
}
-(UILabel *)lwc_nameLabel
{
    return LS_LAZY(_lwc_nameLabel, ({
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.lwc_headImageView.frame)+10,CGRectGetMinY(self.lwc_headImageView.frame), kTextWidth, 20)];
        label.backgroundColor=[UIColor clearColor];
        label.font=[UIFont systemFontOfSize:16];
        label.textColor=[UIColor blackColor];
        [self.contentView addSubview:label];
        label;
    }));
}

-(UILabel *)lwc_timeLabel
{
    return LS_LAZY(_lwc_timeLabel, ({
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.lwc_headImageView.frame)+10,CGRectGetMaxY(self.lwc_nameLabel.frame)+5, kTextWidth,15 )];
        label.backgroundColor=[UIColor clearColor];
        label.font=[UIFont systemFontOfSize:13];
        label.textColor=[UIColor lightGrayColor];
        [self.contentView addSubview:label];
        label;
    }));
}
-(UIButton *)lwc_deleteBtn
{
    return LS_LAZY(_lwc_deleteBtn, ({
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-50, 10, 40, 40)];
        button.backgroundColor = [UIColor clearColor];
        button.hidden = YES;
        button.titleLabel.font = LS_Font(13);
        [button setImage:LS_Getimage(@"delete") forState:UIControlStateNormal];
        [self.contentView addSubview:button];
        button;
    }));
}
-(UILabel *)lwc_linkLabel
{
    return LS_LAZY(_lwc_linkLabel, ({
        UILabel *label = [[UILabel alloc]init];
        label.font = LS_Font(13);
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        [self.contentView addSubview:label];
        label;
    }));
}
-(UIView *)lwc_lineView
{
    return LS_LAZY(_lwc_lineView, ({
        UIView *lineView = [[UIView alloc]init];
        [self addSubview:lineView];
        lineView.backgroundColor=LS_SetAColor(235, 235, 235, 1);
        lineView;
    }));
}
-(LImageListView *)lwc_listView
{
    return LS_LAZY(_lwc_listView, ({
        LImageListView *lwc_listView = [[LImageListView alloc] init];
        [self.contentView addSubview:lwc_listView];
        lwc_listView;
    }));
}
-(BZFireworkAnimationButton *)praiseButton
{
    return LS_LAZY(_praiseButton, ({
        BZFireworkAnimationButton *button = [[BZFireworkAnimationButton alloc]init];
        [self.contentView addSubview:button];
        button.hidden = YES;
        button.particleImage = [UIImage imageNamed:@"color_fabulous"];
        button.particleScale = 0.05f;
        button.particleScaleRange = 0.02f;
        button.titleLabel.font = LS_Font(13);
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:MainColor forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:@"givethethumbs-up"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"color_fabulous"] forState:UIControlStateSelected];
        button;
    }));
}
-(HJLayoutBtn *)lwc_prasieButton
{
    return LS_LAZY(_lwc_prasieButton, ({
      HJLayoutBtn *button=[[HJLayoutBtn alloc]init];
        button.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:button];
        button.HJ_Style = HJLaoutBtnStyleImageLeft;
        button.HJ_Spacing = 4.0;
        [button setImage:[UIImage imageNamed:@"givethethumbs-up"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"color_fabulous"] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:MainColor forState:UIControlStateSelected];
        button.titleLabel.font = LS_Font(13);
        button;
    }));
}
-(HJLayoutBtn *)lwc_commentButton
{
    return LS_LAZY(_lwc_commentButton, ({
        HJLayoutBtn *button=[[HJLayoutBtn alloc]init];
        button.backgroundColor=[UIColor clearColor];
        [self.contentView addSubview:button];
        button.HJ_Style = HJLaoutBtnStyleImageLeft;
        button.HJ_Spacing = 5.0;
        [button setImage:[UIImage imageNamed:@"comment"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = LS_Font(13);
        button;
    }));
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
